import 'dart:io';
import 'package:flutter_background_service/flutter_background_service.dart';
import 'package:huxiaodui/api/api.dart';
import 'package:huxiaodui/background_run/bg_channel.dart';
import 'package:huxiaodui/components/button_item.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:huxiaodui/utils/aliyun_push.dart';
import 'package:package_info_plus/package_info_plus.dart';

import '../../store/set.dart';

class SettingPage extends StatefulWidget {
  const SettingPage({super.key});

  @override
  State<StatefulWidget> createState() => SettingPageState();
}

class SettingPageState extends State<SettingPage> {
  PackageInfo? packageInfo;
  @override
  void initState() {
    super.initState();
    getPackageInfo();
  }

  getPackageInfo() async {
    packageInfo = await PackageInfo.fromPlatform();
    setState(() {});
  }

  void deleteDirectory(String directoryPath) async {
    try {
      var directory = Directory(directoryPath);
      await directory.delete(recursive: true);
      if (await directory.exists()) {
        print(directoryPath);
      }
    } catch (e) {
      print(e.toString());
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("设置"),
      ),
      body: Center(
        child: Container(
          padding: const EdgeInsets.only(top: 20),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Column(
                children: [
                  // Visibility(
                  //   visible: Platform.isAndroid,
                  //   child: ButtonItem(
                  //     iconData: Icons.backup_table_rounded,
                  //     title: "后台定位",
                  //     onTap: () async {
                  //       await SetBackstagePositioning.negate();
                  //       FlutterBackgroundService().invoke(BgChannelType.reSurePosition);
                  //       setState(() {});
                  //     },
                  //     rWidget: FutureBuilder<bool>(
                  //       future: SetBackstagePositioning.get(),
                  //       builder: (context, snapshot) {
                  //         return Switch(
                  //           value: snapshot.data ?? false,
                  //           onChanged: (value) async {
                  //             if (value) {
                  //               await SetBackstagePositioning.open();
                  //             } else {
                  //               await SetBackstagePositioning.close();
                  //             }
                  //             FlutterBackgroundService().invoke(BgChannelType.reSurePosition);
                  //             setState(() {});
                  //           },
                  //         );
                  //       },
                  //     ),
                  //   ),
                  // ),
                  ButtonItem(
                    iconData: Icons.upload_rounded,
                    title: "检查更新",
                    rWidget: Row(
                      children: [
                        Text(
                          'Version ${packageInfo?.version ?? ""}',
                          style: TextStyle(fontSize: 16),
                        ),
                        SizedBox(width: 24),
                        const Icon(
                          Icons.arrow_forward_ios,
                        ),
                      ],
                    ),
                    onTap: () {
                      Api.other.updateApp(isToast: true);
                    },
                  ),
                ],
              ),
              Container(
                margin: EdgeInsets.only(bottom: 12),
                child: SelectableText(SystemPush.deviceId),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
